<?php

class Eleven_Model_Picture extends CMS_Model_Acl_Abstract
{
	public function getCatepics()
	{
		return $this->getResource('Catepic')->getCatePics();
	}
	
	public function deleteCatepicById($cateId)
	{
		return $this->getResource('Catepic')->deleteCatepicById($cateId);
	}
	
	public function addCate($post)
	{
	    $form = $this->getForm('pictureCate');
       	return $this->_addCate($form, $post);
	}
	
	protected function _addCate($form, $info)
	{
	    if (!$form->isValid($info)) {
            return false;
        }
        
        $this->getResource('Catepic')->saveRow($info);
	}
	
    public function getPictures($cateId=NULL, $paged=null, $order=null)
    {
        return $this->getResource('Picture')->getPictures($cateId, $paged, $order);
    }
    
    public function getIndexPics($total=4)
    {
        return $this->getResource('Picture')->getPics($total);
    }
    
    public function getPictureById($id)
    {
        return $this->getResource('Picture')->getPictureById($id);
    }
    
    public function deletePictureById($id)
    {
        return $this->getResource('Picture')->deletePictureById($id);   
    }
    
	public function savePicture($post)
	{
		$form = $this->getForm('pictureUpload');
       	return $this->_save($form, $post);
	}
	
	protected function _save($form, $info, $defaults=array())
    {       
        if (!$form->isValid($info)) {
            return false;
        }
   
		$uploadedData = $form->getValues();
		$fullFilePath1 = $form->pic1->getFileName();
		$fullFilePath2 = $form->pic2->getFileName();
		$fullFilePath3 = $form->pic3->getFileName();
		
		$desc1 = $form->desc1->getValue();
		$desc2 = $form->desc2->getValue();
		$desc3 = $form->desc3->getValue();
		
		$cate = $form->cate->getValue();
		
		require_once '../library/phpthumb/ThumbLib.inc.php';  
		
        if ($fullFilePath1)
		{
    		$this->getResource('picture')->saveRow($this->_thumb($fullFilePath1,$desc1,$cate));
		}
		
		if ($fullFilePath2)
		{
		    $this->getResource('picture')->saveRow($this->_thumb($fullFilePath2,$desc2,$cate));
		}
		
    	if ($fullFilePath3)
		{
		    $this->getResource('picture')->saveRow($this->_thumb($fullFilePath3,$desc3,$cate));
		}
    }
    
    private function _thumb($fullFilePath, $desc,$cate)
    {
        $locationFile = $fullFilePath;
        $ext=end(explode(".",$locationFile));
        $currentMicroTime = Internim_Date::getMicrotime();
        $nameFile = $currentMicroTime.'.'.$ext;
        $fullPathNameFile = './data/uploads/'.$nameFile;
        $thumbPathNameFile = './data/uploads/thumb/'.$nameFile;
        
        $filterRename = new Zend_Filter_File_Rename(array('target'=> $fullPathNameFile, 'overwrite' => true));
        $filterRename->filter(iconv('UTF-8', 'GB2312', $locationFile));
        
        $target = $filterRename->getFile();
                
        $thumb = PhpThumbFactory::create($fullPathNameFile);  
        $thumb->resize(129, 98)->save($thumbPathNameFile);
        
        $target = $filterRename->getFile();
        
        return array('filename'=>$fullPathNameFile, 'description'=>$desc, 'thumb'=>$thumbPathNameFile, 'cate'=>$cate);
    }
	/**
     * @param CMS_Acl_Interface $acl
     */
    public function setAcl (CMS_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)
                ->allow('Guest', $this, array('index'))
                ->allow('Student', $this, array('upload'))
                ->allow('Teacher');
                
            $this->_acl = $acl;
            return $this;
        }
    }

/**
     * 
     */
    public function getAcl ()
    {
        if (null === $this->_acl)
        {
            $this->setAcl(new Eleven_Model_Acl());
        }
        
        return $this->_acl; 
    }

	/**
     * 
     */
    public function getResourceId ()
    {
        return 'Picture';
    }


}

?>